#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

const std::string filename = "./fifo";
const int fileread = O_RDONLY;
const int filewrite = O_WRONLY;
const int readsize = 1024;
const mode_t fmode = 0600;
const int defaultfd = -1;

static int fd;

//读的方式打开文件
int OpenPipeRead()
{
    fd = ::open(filename.c_str(), fileread);
    if (fd < 0)
    {
        std::cerr << "open error" << std::endl;
        return -1;
    }
    return fd;
}

//写的方式打开文件
int OpenPipeWrite()
{
    fd = ::open(filename.c_str(), filewrite);
    if (fd < 0)
    {
        std::cerr << "open error" << std::endl;
        return -1;
    }
    return fd;
}

bool ClosePipe()
{
    if (fd >= 0)
    {
        int n = ::close(fd);
        if (n < 0)
        {
            std::cerr << "close error" << std::endl;
            return false;
        }
        return true;
    }
    return false;
}
